#include "stdafx.h"
#include "SoundDevice.h"

namespace Framework {

SoundDevice::SoundDevice() {
    this->directSound = NULL;
    this->listener = NULL;
}

SoundDevice::~SoundDevice(void) {
    d3d::Release(this->directSound);
}

IDirectSound8* SoundDevice::GetDirectSound() const {
    return this->directSound;
}

IDirectSound3DListener8* SoundDevice::GetListener() const {
    return this->listener;
}

void SoundDevice::Initialize(HWND hwnd) {
    DirectSoundCreate8(NULL, &this->directSound, NULL);
    this->directSound->SetCooperativeLevel(hwnd, DSSCL_PRIORITY);
    DSBUFFERDESC dsbd;
    ZeroMemory(&dsbd, sizeof(DSBUFFERDESC));
    dsbd.dwSize = sizeof(DSBUFFERDESC);
    dsbd.dwFlags = DSBCAPS_CTRL3D | DSBCAPS_PRIMARYBUFFER;

    IDirectSoundBuffer* buffer = NULL;
    HRESULT hr = this->directSound->CreateSoundBuffer(&dsbd, (LPDIRECTSOUNDBUFFER*)&buffer, NULL);
    if (hr == S_OK) {
        buffer->QueryInterface(IID_IDirectSound3DListener8, (LPVOID *)&this->listener);
        this->listener->SetRolloffFactor(0.1f, DS3D_DEFERRED);
        d3d::Release(buffer);
    }
}

}
